import { marked } from "marked";
import $ from 'jquery'


/* 
  let mc = msg('123123', true)

  let i = 0
  setInterval(() => {

    mc.setText(i++)
  }, 100)

  setTimeout(() => {
    mc.del(() => { console.log('done') })
  }, 5000)

*/
function msg(obj, getElem) {
  const init = {
    text: '',
    title: '提示',
    delay: 2000,
    allowClick: false,
    zt: null
  }

  if (typeof obj == 'string' || typeof obj == 'number') {
    init.text = obj.toString()
  } else {
    for (let k in obj) {
      init[k] = obj[k]
    }
  }

  init.text = init.text.toString()

  let msg_wrap = $('.msg_wrap')

  if (!msg_wrap.length) {
    msg_wrap = $(`<div class="msg_wrap markdown-body" style="margin-left: 0.4rem;margin-bottom: 0.3rem;height: unset;padding: unset;background: unset;overflow: unset;box-shadow: unset;">`)
    $(document.body).append(msg_wrap)
  }

  const is_first = msg_wrap.find('*').length == 0
  const msg_content = $(`<div class="msg_content">`)

  const mc = msg_content.get()

  mc.title = init.title

  mc.setTitle = text => {
    msg_content.find('h4').remove()

    const title_elem = $(`<h4>${text}</h4>`)
    msg_content.append(title_elem)
  }

  mc.setText = (text, title) => {
    mc.setTitle(title ? title : mc.title)
    msg_content.find('p').remove()

    const md = $(marked.parse(text.toString()))
    md.find('*').each((i, e) => {
      if (e.innerHTML == '') {
        e.remove()
      }
    })
    const msg_elem = $(`<p>${md.html()}</p>`)
    msg_content.append(msg_elem)
  }

  msg_wrap.append(msg_content)

  const mc_w = msg_content.outerWidth()
  const mc_h = msg_content.outerHeight()

  let msg_w = ''
  {
    // 手动设置大小
    const clone_wrap = $(msg_wrap[0].cloneNode()).html('')
    const clone_contnet = $(msg_content[0].cloneNode(true))

    clone_wrap.append(clone_contnet)
    $(document.body).append(clone_wrap)

    msg_w = clone_contnet.outerWidth()
    clone_wrap.remove()
  }


  mc.show = () => {
    msg_content.css({
      marginLeft: -mc_w + 'px',
      marginBottom: is_first ? 0 : -mc_h + 'px',
      pointerEvents: init.allowClick ? 'auto' : 'none',
      width: msg_w / parseFloat(document.documentElement.style.fontSize) + 'rem'
    }).animate({
      marginLeft: '0.4rem',
      marginBottom: '0.3rem'
    })
  }

  mc.del = callback => {
    msg_content.delay(init.delay).animate({
      marginLeft: -mc_w + 'px',
      opacity: 0
    }, () => {
      setTimeout(() => {
        msg_content.remove()

        callback && callback()
      }, 500)

    })
  }

  if (getElem) {
    mc.show()
    mc.setText(init.text)
    mc.setText(init.title)
    return mc
  } else {
    mc.show()
    mc.del()
    mc.setTitle(init.title)
    mc.setText(init.text)
  }


  return new Promise(resolve => setTimeout(() => resolve(), init.zt ?? init.delay))
}

function downloadJSON(data, filename = 'data', space = 2) {
  const jsonString = JSON.stringify(data, null, space);

  // 创建Blob对象并生成URL
  const blob = new Blob([jsonString], { type: 'application/json' })
  const url = URL.createObjectURL(blob)

  const a = document.createElement('a')
  a.style.display = 'none'
  a.href = url
  a.download = `${filename}.json`

  document.body.appendChild(a);
  a.click();

  // 清理资源
  document.body.removeChild(a);
  URL.revokeObjectURL(url);
}

export { msg, downloadJSON }